<template>
    <div class="app-container">
      <el-form ref="form" :model="form" label-width="130px" :rules="rules" :inline="action == 'check' ? true : false" :disabled="action == 'check' ? true : false">
        <el-form-item label="业务类型:" prop="type">
            <el-select v-model="form.type" placeholder="请选择业务类型" style="width: 260px;">
                <el-option v-for="i in businessOptions" :key="i.key" :label="i.name" :value="i.key"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="银行:" prop="bankId">
            <el-select v-model="form.bankId" placeholder="请选择银行" filterable  style="width: 260px;">
                <el-option v-for="i in bankOptions" :key="i.id" :label="i.name" :value="i.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="委托合同号:" prop="contractNo" >
          <el-input style="width: 260px;" v-model="form.contractNo" placeholder="请输入委托合同号"></el-input>
        </el-form-item>
        <el-form-item label="金额(万元):" prop="money">
          <el-input style="width: 260px;" type="number" v-model="form.money" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="企业名称:" prop="entName">
          <el-select
            style="width: 260px;"
            @change="entNameChange"
            v-model="form.entName"
            filterable
            remote
            placeholder="请输入企业名称"
            :remote-method="remoteMethod"
            :loading="entNameLoading">
            <el-option
              v-for="item in entNameOptions"
              :key="item.uniscid"
              :label="item.name"
              :value="item.uniscid + '@name' + item.name + '@region' + item.region">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区县:" prop="region">
            <el-select v-model="form.region" placeholder="请选择区县" style="width: 260px;">
                <el-option v-for="i in cityOptions" :key="i.key" :label="i.name" :value="i.name"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="统一信用代码:" prop="entUniId">
          <el-input style="width: 260px;"  v-model="form.entUniId" placeholder="请输入统一信用代码"></el-input>
        </el-form-item>

      </el-form>
      <div class="btnGroup">
          <el-button type="primary" v-if="action == 'add' || !action" @click="onSubmit">录入</el-button>
          <el-button type="success" v-if="action == 'edit'" @click="onSubmit">确定修改</el-button>
      </div>

      <Table 
          class="businessFormTable"
          ref="Table" 
          :tableData="tableData" 
          :tableHeadData="tableHeadData" 
          v-loading="tableLoading"
          @getList="getTableList" 
          :pageHidden="true" 
          v-if="action == 'check'"
      />
    </div>
  </template>
  
  <script>
  import { cityOptions,businessOptions } from '@/utils/index'
  import { getBanks,getEnterprise,addEnterprise,getBusinessById,updateEnterprise,contractlistByBz,getSignUrl } from '@/api/user'
  import Table from '@/components/table/table.vue';
  import { XinrunCommon } from './juece.common'
  export default {
    components:{
      Table
    },
    data() {
      return {
        form: {
          type: '',
          region: '',
          bankId:'',
          contractNo:'',
          money:'',
          entUniId:'',
          entName:''
        },
        rules:{
          type:[{ required: true, message: '请选择业务类型', trigger:[ 'blur','change'] }],
          bankId:[{ required: true, message: '请选择银行', trigger:[ 'blur','change'] }],
          contractNo:[{ required: true, message: '请输入委托合同号', trigger:[ 'blur','change'] }],
          money:[{ required: true, message: '请输入金额', trigger:[ 'blur','change'] }],
          entName:[{ required: true, message: '请输入企业名称', trigger:[ 'blur','change'] }],
          region:[{ required: true, message: '请选择区县', trigger:[ 'blur','change'] }],
          entUniId:[{ required: true, message: '请输入统一信用代码', trigger:[ 'blur','change'] }],
          
        },
        cityOptions:Object.freeze(cityOptions),
        businessOptions:Object.freeze(businessOptions),
        action:'',
        fileList:[],
        files:[],
        bankOptions:[],
        entNameOptions:[],
        isReview:false,
        entNameLoading:false,
        tableData:[],
        tableHeadData:[],
        tableLoading:false
      }
    },
    mounted(){
      this.xinrunCommon = new XinrunCommon(this);
      this.getBanks()
    },
    activated(){
      this.init();
    },
    methods: {
      init(){
        //console.log(' this.$router', this.$route)
        //this.$route.meta.title = 'adw'
        const { action, id } = this.$route.query;
        this.listId = id;
        this.action = action;
        if(action === 'edit'){
          this.$route.meta.title = '业务信息编辑';
          
          this.render(id)
        }else if(action === 'check'){
          this.$route.meta.title = '业务信息查看';
          this.render(id,true)
        }else{
          this.$route.meta.title = '业务信息录入';
          this.$refs['form'].resetFields();
        }
        //
      },
      async getBanks(){
        const data = await getBanks();
        this.bankOptions = data.data;
        console.log('data',data)
      },
      async remoteMethod(query) {
        if (query !== '') {
          this.entNameLoading = true;
          const params = {
            name:query
          }
          const res = await getEnterprise(params);
          //console.log('res',res)
          if(res.message === 'SUCCESS')
            this.entNameLoading = false;
            this.entNameOptions = res.data;
            // setTimeout(() => {
            //   this.entNameLoading = false;
            //   this.entNameOptions = this.list.filter(item => {
            //     return item.label.toLowerCase()
            //       .indexOf(query.toLowerCase()) > -1;
            //   });
            // }, 200);
          } else {
            this.entNameOptions = [];
          }
      },  
      entNameChange(data){
        //console.log('entNameChange',data);
        const entUniId = data.slice(0,data.indexOf('@name'));
        const region = data.slice(data.indexOf('@region') + 7,data.length);
        const entName = data.slice(data.indexOf('@name') + 5,data.indexOf('@region'));
        this.form.entUniId = entUniId;
        this.form.region = region;
        this.form.entName = entName
        //console.log('entUniId',entUniId);
        //console.log('region',region);
      },
      onSubmit() {
        this.$refs['form'].validate(async(valid) => {
          if (valid) {
            //console.log('this。form',this.form);
            const data = {
              ...this.form
            }
            console.log('data',data);
            if(this.action === 'edit'){
              const res = await updateEnterprise({...data,id:this.listId})
              //console.log('res',res);
              const { message } = res;
              if(message === 'SUCCESS'){
                this.$message('修改成功');
                this.$router.push('/businessList')
              }
            }else{
              const res = await addEnterprise(data)
              //console.log('res',res);
              const { message } = res;
              if(message === 'SUCCESS'){
                this.$message('添加成功');
                this.$router.push('/businessList')
              }
            }
            
          } 
        }); 
        //this.$message('submit!')
      },
      async render(id,renderTable) {
        const data = await getBusinessById({id});
        if(data.message === 'SUCCESS'){
          const { contractNo,entName,bankId,entUniId,money,region,type } = data.data;
          this.form = {
            contractNo,
            entName,
            bankId,
            entUniId,
            money,
            region,
            type
          };
        };
        //console.log('getBusinessById', data);
        if(renderTable){
          this.getTableList(id)
        }
        
      },
      async getTableList(id){
        this.tableLoading = true;

        const data = await contractlistByBz({bzId:id});
        //const data = await this.iDMapServer.getTestData('taskList');
        //console.log('data',data)
        this.tableData = data.data;
        this.data = this.xinrunCommon.fomatData(data.data);
        //this.total = data.total;

        this.tableHeadData = this.xinrunCommon.createTableHeadData(data.data[0],this);
        this.tableLoading = false;
      },
      async checkGetSignUrl(row){
        this.xinrunCommon.turnOnLoading('.businessFormTable' ,'链接加载中，请稍后')
        const { id } = row;
        try{
          const res = await getSignUrl({id});
          this.xinrunCommon.closeLoading();
          console.log('getSignUrl',res);
          this.$confirm(res.data, '', {
            confirmButtonText: '一键复制链接',
          }).then(() => {
            this.$copyText(res.data).then(e => {
                this.$message({
                  message: '已复制到剪贴板',
                  type: 'success'
                });
              })
          }).catch(() => {});
        }catch(err){
          this.xinrunCommon.closeLoading();
        }
      }
    }
  }
  </script>
  
  <style scoped lang="scss">
  .btnGroup{
    margin-top: 25px;
    display: flex;
    justify-content: center;
  }
  </style>
  
  